﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace OnlineFloralShop.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get link Referrer login
            if (!Page.IsPostBack)
            {
                if (Page.Request.UrlReferrer != null)
                {
                    Session["LoginReferrer"] = Page.Request.UrlReferrer.ToString();
                }
            }
            //------------------------------------------------------------


            //if loged on redirect to Home page
            if (User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Default.aspx");
            }
            if (!Page.IsPostBack)
            {
                txtuser.Text = "";
                txtpass.Text = "";
            }

            //Set link Register page
            if (Request.QueryString["ReturnUrl"] != null)
            {
                a_register.HRef = "~/Account/WebForm1.aspx?ReturnUrl=" + Request.QueryString["ReturnUrl"];
            }
            else
            {
                a_register.HRef = "~/Account/WebForm1.aspx";
            }
        }



        protected void btnSignin_Click(object sender, EventArgs e)
        {

            if (Membership.ValidateUser(txtuser.Text, txtpass.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(txtuser.Text, CheckBox1.Checked);

                //Redirect sau khi login 
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    Response.Redirect(Request.QueryString["ReturnUrl"]);
                }
                else if (Session["LoginReferrer"] != null)
                {
                    Response.Redirect(Session["LoginReferrer"].ToString());
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                lbMessage.Text = "UserName or Password is not correct!";
            }
        }
    }
}
